package org.iteam.core.service;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.iteam.db.DBPoolFactory;

public class DBService {
	
	public static DataSource getDataSource() {
		return DBPoolFactory.getInstance().getDataSource(DBPoolFactory.MYSQL);
	}

	public static QueryRunner getRunner() {
		return new QueryRunner(getDataSource());
	}
	// public static DataSource getDataSource() {
	// if (Constant.dataBaseType.equals(Constant.DataBaseType.MYSQL))
	// return DBPoolFactory.getInstance().getDataSource(
	// DBPoolFactory.MYSQL);
	// else {
	// if (!DBPoolFactory.getInstance().containsKey(
	// Constant.DataBaseType.ORACLE.ecode))
	// addOracle();
	// return DBPoolFactory.getInstance().getDataSource(
	// Constant.DataBaseType.ORACLE.ecode);
	// }
	// }
	//
	// public static void addOracle() {
	// DBPoolFactory.getInstance().add(Constant.DataBaseType.ORACLE.ecode,
	// "oracle.jdbc.driver.OracleDriver",
	// "jdbc:oracle:thin:@localhost:1521/ORCL", "vitco", "vitco");
	// }

	/**
	 * 10*2 10*(2-1)
	 * 
	 * @param sql
	 * @return
	 */
	public static String toOraclePage(String sql) {
		return "SELECT * FROM (SELECT MY_TABLE.*,ROWNUM TABLE_SN FROM ( " + sql
				+ ") MY_TABLE WHERE ROWNUM <= ? ) WHERE TABLE_SN > ?";
	}

	public static String toOraclePage(String sql, long page, long rows) {
		return "SELECT * FROM (SELECT MY_TABLE.*,ROWNUM TABLE_SN FROM ( " + sql
				+ ") MY_TABLE WHERE ROWNUM <= " + page * rows
				+ " ) WHERE TABLE_SN > " + rows * (page - 1);
	}
}
